﻿package com.ui.containers
{
	import com.ui.controls.Button;
	import com.ui.core.INewHelp;
	import com.ui.core.UIComponent;
	import com.ui.data.TitleWindowData;
	import com.ui.manager.UIManager;
	import com.ui.manager.ViewManage;
	import com.ui.skin.ASSkin;
	import com.utils.MathUtil;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * @version 20091215
	 * @author Cafe
	 */
	public class TitleWindow extends UIComponent implements INewHelp
	{
		protected var _data:TitleWindowData;
		protected var _titleBar:TitleBar;
		protected var _contentPanel:Panel;
		protected var _modalSkin:Sprite;
		protected var _regX:int;
		protected var _regY:int;
		protected var _closeButton:Button;
		protected var _helpNotice:DisplayObject;
		protected var _callLater:Function;

		override protected function create():void
		{
			_titleBar = new TitleBar( _data.titleBarData );
			_titleBar.width = _data.panelData.width;
			_titleBar.x = 0;
			_contentPanel = new Panel( _data.panelData );
			_contentPanel.y = _titleBar.height - 1;
			addChild( _contentPanel );
			addChild( _titleBar );
			if (_data.modal)
			{
				_modalSkin = ASSkin.modalSkin;
			}
			addCloseButton();
		}

		protected function addCloseButton():void
		{
			if (_data.closeButtonData != null)
			{
				_closeButton = new Button( _data.closeButtonData );
				_titleBar.addChild( _closeButton );
				_closeButton.addEventListener( MouseEvent.CLICK, cloesButton_handler );
			}
		}

		protected function cloesButton_handler( event:MouseEvent ):void
		{
			dispatchEvent( new Event( Event.CLOSE ) );
			hide();
		}

		override protected function layout():void
		{
			_contentPanel.setSize( _width, _height - _titleBar.height - 1 );
			_closeButton.y = (_titleBar.height - _closeButton.height) * .5;
			_closeButton.x = _width - _closeButton.width - 10 ;
		}

		override protected function onShow():void
		{
			if (_data.modal)
			{
				var topLeft:Point = parent.localToGlobal( MathUtil.ORIGIN );
				_modalSkin.x = -topLeft.x + UIManager.stageOffset.x;
				_modalSkin.y = -topLeft.y + UIManager.stageOffset.y;
				_modalSkin.width = UIManager.root.stage.stageWidth;
				_modalSkin.height = UIManager.root.stage.stageHeight;
				parent.addChildAt( _modalSkin, parent.numChildren - 1 );
				parent.swapChildrenAt( parent.getChildIndex( this ), parent.numChildren - 1 );
			}
			if (_data.allowDrag)
			{
				_titleBar.addEventListener( MouseEvent.MOUSE_DOWN, titleBar_mouseDownHandler );
			}
			if (_data.isClickTop) ViewManage.add( this );
		}

		override protected function onHide():void
		{
			if (_data.modal && _modalSkin.parent)
			{
				_modalSkin.parent.removeChild( _modalSkin );
			}
			if (_data.allowDrag)
			{
				_titleBar.removeEventListener( MouseEvent.MOUSE_DOWN, titleBar_mouseDownHandler );
				if (stage.hasEventListener( MouseEvent.MOUSE_MOVE ))
				{
					stage.removeEventListener( MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler );
				}
				if (stage.hasEventListener( MouseEvent.MOUSE_UP ))
				{
					stage.removeEventListener( MouseEvent.MOUSE_UP, stage_mouseUpHandler );
				}
			}
			if (_data.isClickTop) ViewManage.remove( this );
		}

		protected function titleBar_mouseDownHandler( event:MouseEvent ):void
		{
			startDragging( event );
		}

		private function startDragging( event:MouseEvent ):void
		{
			_regX = event.stageX - x;
			_regY = event.stageY - y;
			stage.addEventListener( MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler );
			stage.addEventListener( MouseEvent.MOUSE_UP, stage_mouseUpHandler );
		}

		private function stage_mouseMoveHandler( event:MouseEvent ):void
		{
			if (isNaN( _regX ) || isNaN( _regY )) return;
			event.updateAfterEvent();
			var newX:int = event.stageX - _regX;
			// Math.min( stage.stageWidth - _width, event.stageX - _regX );
			var newY:int = event.stageY - _regY;
			// Math.min( stage.stageHeight - _height, event.stageY - _regY );
			// if (newX < 0)
			// {
			// newX = 0;
			// }
			// if (newY < 0)
			// {
			// newY = 0;
			// if (_titleBar.y != 0) newY -= _titleBar.height;
			// }
			newX = newX < -width / 2 ? -width / 2 : newX;
			newY = newY + _titleBar.height < 0 ? -_titleBar.height : newY;

			newX = newX > UIManager.root.stage.stageWidth - width / 2 ? UIManager.root.stage.stageWidth - width / 2 : newX;
			newY = newY > UIManager.root.stage.stageHeight - _titleBar.height * 2 ? UIManager.root.stage.stageHeight - _titleBar.height * 2 : newY;
			moveTo( newX, newY );
		}

		private function stage_mouseUpHandler( event:MouseEvent ):void
		{
			if (!isNaN( _regX )) stopDragging();
		}

		private function stopDragging():void
		{
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler );
			stage.removeEventListener( MouseEvent.MOUSE_UP, stage_mouseUpHandler );
			_regX = NaN;
			_regY = NaN;
		}

		public function TitleWindow( data:TitleWindowData )
		{
			_data = data;
			_data.titleBarData.width = _data.width;
			super( data );
		}

		public function get contentPanel():Panel
		{
			return _contentPanel;
		}

		public function get modal():Boolean
		{
			return _data.modal;
		}

		public function resizeModal():void
		{
			if (_modalSkin == null) return;
			_modalSkin.width = UIManager.root.stage.stageWidth;
			_modalSkin.height = UIManager.root.stage.stageHeight;
		}
		
		public function get helpNotice() : DisplayObject {
			return _helpNotice;
		}

		public function set helpNotice(helpNotice : DisplayObject) : void {
			_helpNotice = helpNotice;
		}

		public function get callLater() : Function {
			return _callLater;
		}

		public function set callLater(callLater : Function) : void {
			_callLater = callLater;
		}
	}
}